"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 数据库系统
Case Name   : 恢复pg_xlog目录权限后，重启数据库，数据库启动成功，业务运行正常
Create At   : 2020/06/23
@zou_jialiang0505328126
Description :
    1.恢复pg_xlog权限
    2.重启数据库
    3.执行TPCC业务成功
Expect      :
    1.恢复pg_xlog权限成功
    2.重启数据库成功
    3.执行TPCC业务成功
History     :
    modified：2021/03/17  modified by @peilinqian，根据用例描述，增加stop步骤；
        执行完成tpcc后，增加主备同步等待
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import *
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class ModifyPgxlogPermi(unittest.TestCase):
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('----Opengauss_Reliability_Dbsys_Case046.py start----')
        self.rootNode = Node()
        self.dbUserNode = Node(node='dbuser')
        self.TPCC_PATH = macro.TPCC_PATH
        self.sh = CommonSH()
        self.com = Common()
        self.constant = Constant()
        self.node_num = self.com.get_node_num(self.rootNode)
        self.log.info(self.node_num)
        self.min_time = 300

        self.log.info("----delete xlog permission----")
        modify_permission_cmd = 'chmod -R 000 ' + macro.PG_XLOG_PATH
        self.log.info(modify_permission_cmd)
        modify_msg = self.rootNode.sh(modify_permission_cmd).result()
        self.log.info(modify_msg)

    def test_main(self):
        self.log.info('----recover xlog permission----')
        modify_permission_cmd = 'chmod -R 700 ' + macro.PG_XLOG_PATH
        self.log.info(modify_permission_cmd)
        modify_msg = self.rootNode.sh(modify_permission_cmd).result()
        self.log.info(modify_msg)
        show_permission_cmd = f'ls -al {os.path.dirname(macro.PG_XLOG_PATH)}'
        show_msg = self.rootNode.sh(show_permission_cmd).result()
        self.log.info(show_msg)

        self.log.info('----restart opengauss----')
        is_stop = self.sh.stopDbCluster()
        self.assertTrue(is_stop)
        is_started = self.sh.startDbCluster()
        self.assertTrue(is_started)

        self.log.info('----执行tpcc----')
        tpcc_result = self.com.startTPCC(self.rootNode, self.TPCC_PATH)
        self.log.info(tpcc_result)
        error_flags = ('error', 'ERROR', 'FATAL')
        if self.constant.TPCC_SUCCESS_MSG in tpcc_result:
            self.assertFalse(any(err in tpcc_result for err in error_flags))
        else:
            self.assertTrue(False)

        self.log.info('----查看主机query，同步是否正常----')
        self.sh.check_location_consistency('primary',
                                           self.node_num,
                                           self.min_time)

        self.log.info('----查询主备状态----')
        status = self.sh.getDbClusterStatus('status')
        self.assertTrue(status)

    def tearDown(self):
        self.log.info('----this is tearDown----')
        show_permission_cmd = 'chmod -R 700 ' + macro.PG_XLOG_PATH
        self.log.info(show_permission_cmd)
        modify_msg = self.rootNode.sh(show_permission_cmd).result()
        self.log.info(modify_msg)
